home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / datatypes / png_dt / viewdt / viewdt.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  12KB  |  403 lines

  1. /*
  2.    ViewDT
  3.  
  4.    Simple DataType-based Picture Viewer
  5.  
  6.    Syntax:
  7.       ViewDT FILES/A
  8.  
  9.    Examples:
  10.       ViewDT pictures:space/#?.pic
  11.       ViewDT portrait.png
  12.  
  13.    Source Code Version:
  14.       $VER: ViewDT.c 43.0
  15.  
  16.    Status:
  17.       Public Domain
  18.  
  19.    If you require more information please send E-mail to <info@cloanto.it>
  20. */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <graphics/gfx.h>
  25. #include <graphics/displayinfo.h>
  26. #include <intuition/intuitionbase.h>
  27. #include <intuition/gadgetclass.h>
  28. #include <datatypes/datatypes.h>
  29. #include <datatypes/datatypesclass.h>
  30. #include <datatypes/pictureclass.h>
  31. #include <proto/exec.h>
  32. #include <proto/dos.h>
  33. #include <proto/graphics.h>
  34. #include <proto/intuition.h>
  35. #include <proto/datatypes.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38.  
  39. struct Picture
  40. {
  41.    struct BitMapHeader bmhd;  /* format and infos */
  42.    struct BitMap *bmap;       /* bitmap */
  43.    ULONG *palette;            /* color table in LoadRGB32() format */
  44.    LONG palette_size;         /* mem usage */
  45.    LONG palette_entries;      /* number of colors */
  46.    ULONG display_ID;          /* video mode */
  47.    UBYTE *author;             /* author info */
  48.    UBYTE *copyright;          /* copyright info */
  49.    UBYTE *annotation;         /* other info */
  50.    LONG author_size;          /* mem usage */
  51.    LONG copyright_size;       /* mem usage */
  52.    LONG annotation_size;      /* mem usage */
  53. };
  54.  
  55. void FreePicture(struct Picture *pic);
  56. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG);
  57. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size);
  58. BOOL ViewPicture(struct Picture *pic);
  59.  
  60. struct IntuitionBase *IntuitionBase;
  61. struct GfxBase *GfxBase;
  62. struct Library *DataTypesBase;
  63.  
  64.  
  65.  
  66. /*
  67.    IsDataTypes
  68.  
  69.    Parameters
  70.       file_name: name of the file to inspect
  71.       name_buff: (optional) buffer to store the file format name
  72.       nbuff_size: size of name_buff
  73.  
  74.    Return value
  75.       TRUE if DataTypes recognized the file as a valid picture file
  76.       FALSE otherwise
  77. */
  78. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
  79. {
  80.    struct DataType *dtn;
  81.    struct DataTypeHeader *dth;
  82.    BPTR lock;
  83.    BOOL it_is;
  84.  
  85.    it_is = FALSE;
  86.    if (lock = Lock(file_name, ACCESS_READ))
  87.    {
  88.       /* inspect file */
  89.       if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
  90.       {
  91.          dth = dtn->dtn_Header;
  92.          if (dth->dth_GroupID == GID_PICTURE)   /* is it a picture? */
  93.          {
  94.             it_is = TRUE;
  95.             if (name_buff)
  96.             {
  97.                strncpy(name_buff, dth->dth_Name, nbuff_size);
  98.                *(name_buff + nbuff_size - 1) = 0;  /* safe strncpy() termination */
  99.             }
  100.          }
  101.          ReleaseDataType(dtn);
  102.       }
  103.       UnLock(lock);
  104.    }
  105.    return(it_is);
  106. }
  107.  
  108. /*
  109.    GetDataTypesPicture
  110.  
  111.    Parameters
  112.       file_name: name of the file to load
  113.       pic: work structure
  114.       bmap_flags: AllocBitMap() flags (BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
  115.  
  116.    Return value
  117.       0 if successful (picture info and data in "pic" structure) or
  118.       error code as from dos.library IoErr() function
  119. */
  120. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags)
  121. {
  122.    Object *obj;
  123.    struct BitMapHeader *bmh;
  124.    struct BitMap *bmap;
  125.    struct gpLayout layout;
  126.    ULONG *creg, *ctab;
  127.    UBYTE *str;
  128.    LONG ncol, crsize, err;
  129.  
  130.    memset(pic, 0, sizeof(struct Picture));   /* clear pic structure */
  131.    err = 0;
  132.  
  133.    if (obj = NewDTObject(file_name,
  134.                          DTA_SourceType, DTST_FILE,
  135.                          DTA_GroupID, GID_PICTURE,
  136.                          PDTA_Remap, FALSE,
  137.                          TAG_DONE))    /* get the picture object */
  138.    {
  139.       if (GetDTAttrs(obj,
  140.                      PDTA_ModeID, &pic->display_ID,
  141.                      PDTA_BitMapHeader, &bmh,
  142.                      TAG_DONE) == 2)   /* get the bitmap_header and mode_id */
  143.       {
  144.          pic->bmhd = *bmh;
  145.  
  146.          /*
  147.             query the object about its author, copyright and annotation
  148.          */
  149.          if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
  150.          {
  151.             if (str)
  152.             {
  153.                pic->author_size = strlen(str) + 1;
  154.                if (pic->author = AllocMem(pic->author_size, 0))
  155.                   strcpy(pic->author, str);
  156.             }
  157.          }
  158.          if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
  159.          {
  160.             if (str)
  161.             {
  162.                pic->copyright_size = strlen(str) + 1;
  163.                if (pic->copyright = AllocMem(pic->copyright_size, 0))
  164.                   strcpy(pic->copyright, str);
  165.             }
  166.          }
  167.          if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
  168.          {
  169.             if (str)
  170.             {
  171.                pic->annotation_size = strlen(str) + 1;
  172.                if (pic->annotation = AllocMem(pic->annotation_size, 0))
  173.                   strcpy(pic->annotation, str);
  174.             }
  175.          }
  176.  
  177.          layout.MethodID = DTM_PROCLAYOUT;   /* render the object */
  178.          layout.gpl_GInfo = NULL;
  179.          layout.gpl_Initial = TRUE;
  180.  
  181.          if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
  182.          {
  183.             if (GetDTAttrs(obj,
  184.                            PDTA_BitMap, &bmap,
  185.                            PDTA_CRegs, &creg,
  186.                            PDTA_NumColors, &ncol,
  187.                            TAG_DONE) == 3)   /* get the bitmap and its colors */
  188.             {
  189.                if (bmap != NULL && creg != NULL && ncol != 0)
  190.                {
  191.                   crsize = (ncol * 3) * 4;
  192.                   pic->palette_entries = ncol;
  193.                   pic->palette_size = crsize + (2 * 4);  /* LoadRGB32() table requirements */
  194.  
  195.                   if (pic->palette = AllocMem(pic->palette_size, 0))
  196.                   {
  197.                      ctab = pic->palette;
  198.                      *ctab++ = (ncol << 16) | 0;   /* number of colors and first color to load */
  199.                      memcpy(ctab, creg, crsize);
  200.                      *(ctab + (crsize / 4)) = 0;   /* terminator */
  201.                   }
  202.                   else err = ERROR_NO_FREE_STORE;
  203.  
  204.                   if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, bmap))
  205.                   {
  206.                      BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  207.                      WaitBlit();
  208.                   }
  209.                   else err = ERROR_NO_FREE_STORE;
  210.                }
  211.                else err = ERROR_REQUIRED_ARG_MISSING;
  212.             }
  213.             else err = IoErr();
  214.          }
  215.          else err = IoErr();
  216.       }
  217.       else err = ERROR_REQUIRED_ARG_MISSING;
  218.  
  219.       DisposeDTObject(obj);   /* free the object */
  220.    }
  221.    else err = IoErr();
  222.  
  223.    if (err)
  224.       FreePicture(pic);
  225.  
  226.    return(err);
  227. }
  228.  
  229. /*
  230.    FreePicture
  231.  
  232.    Parameters
  233.       pic: Picture structure with resources to free
  234.  
  235.    Return value
  236.       none
  237. */
  238. void FreePicture(struct Picture *pic)
  239. {
  240.    if (pic->bmap)
  241.    {
  242.       WaitBlit();
  243.       FreeBitMap(pic->bmap);
  244.    }
  245.    if (pic->palette)
  246.       FreeMem(pic->palette, pic->palette_size);
  247.  
  248.    if (pic->author)
  249.       FreeMem(pic->author, pic->author_size);
  250.  
  251.    if (pic->copyright)
  252.       FreeMem(pic->copyright, pic->copyright_size);
  253.  
  254.    if (pic->annotation)
  255.       FreeMem(pic->annotation, pic->annotation_size);
  256.  
  257.    memset(pic, 0, sizeof(struct Picture));   /* clear it all */
  258. }
  259.  
  260. /*
  261.    ViewPicture
  262.  
  263.    Parameters
  264.       pic: picture infos and data
  265.  
  266.    Return value
  267.       TRUE if the user cancelled the view sequence (<Esc> key)
  268.       FALSE otherwise
  269. */
  270. BOOL ViewPicture(struct Picture *pic)
  271. {
  272.    struct Screen *scr;
  273.    struct Window *win;
  274.    struct IntuiMessage *imsg;
  275.    BOOL done, quit;
  276.  
  277.    done = quit = FALSE;
  278.    if (scr = OpenScreenTags(NULL,
  279.                SA_Width, pic->bmhd.bmh_Width,
  280.                SA_Height, pic->bmhd.bmh_Height,
  281.                SA_Depth, pic->bmhd.bmh_Depth,
  282.                SA_Quiet, TRUE,
  283.                SA_ShowTitle, FALSE,    /* no title bar */
  284.                SA_Behind, TRUE,
  285.                SA_Type, CUSTOMSCREEN,
  286.                SA_DisplayID, pic->display_ID,
  287.                SA_Overscan, OSCAN_TEXT,
  288.                SA_AutoScroll, TRUE,
  289.                SA_Colors32, pic->palette,
  290.                SA_BackFill, LAYERS_NOBACKFILL,  /* no screen-clearing when the window is closed (is faster) */
  291.                TAG_END))
  292.    {
  293.       if (win = OpenWindowTags(NULL,
  294.                WA_Width, scr->Width,
  295.                WA_Height, scr->Height,
  296.                WA_IDCMP, MOUSEBUTTONS | VANILLAKEY,
  297.                WA_CustomScreen, scr,
  298.                WA_Backdrop, TRUE,
  299.                WA_Borderless, TRUE,
  300.                WA_Activate, TRUE,
  301.                WA_RMBTrap, TRUE,
  302.                WA_SimpleRefresh, TRUE,
  303.                WA_BackFill, LAYERS_NOBACKFILL,  /* no screen-clearing when the window is opened (is faster) */
  304.                TAG_END))
  305.       {
  306.          BltBitMap(pic->bmap, 0,0, scr->RastPort.BitMap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  307.          WaitBlit();
  308.          ScreenToFront(scr);  /* show the screen only when the picture has been copied to it */
  309.  
  310.          while (!done)
  311.          {
  312.             Wait(1 << win->UserPort->mp_SigBit);
  313.  
  314.             while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  315.             {
  316.                switch (imsg->Class)
  317.                {
  318.                   case VANILLAKEY:
  319.                      switch (imsg->Code)
  320.                      {
  321.                         case 27:  done = quit = TRUE;  break;  /* <Esc> = cancel */
  322.                         case 13:
  323.                         case 32:  done = TRUE;  break;   /* <Enter> / <Space> = continue */
  324.                      }
  325.                      break;
  326.                   case MOUSEBUTTONS:
  327.                      switch (imsg->Code)
  328.                      {
  329.                         case SELECTUP:    /* MButton = continue */
  330.                         case MIDDLEUP:
  331.                         case MENUUP:   done = TRUE;  break;
  332.                      }
  333.                      break;
  334.                }
  335.                ReplyMsg((struct Message *)imsg);
  336.             }
  337.          }
  338.          CloseWindow(win);
  339.       }
  340.       CloseScreen(scr);
  341.    }
  342.    return(quit);
  343. }
  344.  
  345. void main(int argc, char *argv[])
  346. {
  347.    #define AP_BUFFSIZE  240
  348.    #define PT_SIZE      80
  349.    struct Picture pic;
  350.    struct AnchorPath *ap;
  351.    UBYTE pic_type[PT_SIZE];
  352.    LONG err;
  353.    BOOL quit;
  354.  
  355.    if (argc > 1)
  356.    {
  357.       if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39))
  358.       {
  359.          if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
  360.          {
  361.             if (DataTypesBase = OpenLibrary("datatypes.library", 39))
  362.             {
  363.                if (ap = AllocMem(sizeof(struct AnchorPath) + AP_BUFFSIZE, MEMF_CLEAR))
  364.                {
  365.                   ap->ap_Strlen = AP_BUFFSIZE;
  366.                   for (err = MatchFirst(argv[1], ap); err == 0; err = MatchNext(ap))
  367.                   {
  368.                      if (IsDataTypes(ap->ap_Buf, pic_type, PT_SIZE))
  369.                      {
  370.                         if (GetDataTypesPicture(ap->ap_Buf, &pic, 0) == 0)
  371.                         {
  372.                            printf("%s (%s, %dx%d, %d colors)\n",
  373.                                     ap->ap_Info.fib_FileName,
  374.                                     pic_type,
  375.                                     pic.bmhd.bmh_Width,
  376.                                     pic.bmhd.bmh_Height,
  377.                                     pic.palette_entries);
  378.                            if (pic.author)
  379.                               printf("      author: %s\n", pic.author);
  380.                            if (pic.copyright)
  381.                               printf("   copyright: %s\n", pic.copyright);
  382.                            if (pic.annotation)
  383.                               printf("  annotation: %s\n", pic.annotation);
  384.  
  385.                            quit = ViewPicture(&pic);
  386.                            FreePicture(&pic);
  387.                            if (quit)
  388.                               break;
  389.                         }
  390.                      }
  391.                   }
  392.                   MatchEnd(ap);
  393.                   FreeMem(ap, sizeof(struct AnchorPath) + AP_BUFFSIZE);
  394.                }
  395.                CloseLibrary(DataTypesBase);
  396.             }
  397.             CloseLibrary((struct Library *)GfxBase);
  398.          }
  399.          CloseLibrary((struct Library *)IntuitionBase);
  400.       }
  401.    }
  402. }
  403.